package cm.iccsoft.android.telecomcost.adapter;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.activities.ZonesListActivity;
import cm.iccsoft.android.telecomcost.entities.Zone;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ZoneAdapter extends ArrayAdapter<Zone> {
	private ArrayList<Zone> items;
	private Context _context;
	private Activity _activity;

	public ZoneAdapter(Context context, int textViewResourceId,
			ArrayList<Zone> items,Activity activity) {
		super(context, textViewResourceId, items);
		this.items = items;
		_context = context;
		_activity = activity;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public ZoneAdapter(Context context, int resource, int textViewResourceId,
			ArrayList<Zone> objects) {
		super(context, resource, textViewResourceId, objects);
		this.items = objects;
		_context = context;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 */
	public ZoneAdapter(Context context, int resource, int textViewResourceId) {
		super(context, resource, textViewResourceId);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public ZoneAdapter(Context context, int textViewResourceId,
			List<Zone> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public ZoneAdapter(Context context, int textViewResourceId, Zone[] objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 */
	public ZoneAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i("Zone_ListView", "getting zone_row_view");
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(cm.iccsoft.android.telecomcost.R.layout.zone_view,
					null);
		}
		final Zone zone = items.get(position);
		if (zone != null) {			
			final CheckBox chk = (CheckBox) v
					.findViewById(cm.iccsoft.android.telecomcost.R.id.checkBox1);			
			if (chk != null) {	
				chk.setText(zone.toString());
				chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					
					public void onCheckedChanged(CompoundButton arg0, boolean arg1) {						
						selectZone(zone,arg1);
						chk.setChecked(zone.isSelected());
					}
				});
			}	
			
			final Button btnEditZone = (Button)v.findViewById(cm.iccsoft.android.telecomcost.R.id.btnEditZone);
			if(btnEditZone != null)
			{
				btnEditZone.setText("Countries");
				btnEditZone.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						showEditDialog(zone);
					}
				});		
				
				btnEditZone.setEnabled(false);
			}
		}
		return v;
	}
	
	protected void showEditDialog(Zone zone) {
		if(_activity.toString() == "ZonesListActivity")
		{
			Gson gson = TelecomCost.getGson();
			Bundle data = new Bundle();						
			data.putString("zone",gson.toJson(zone));			
			_activity.showDialog(ZonesListActivity.DIALOG_EDITZONE_ID,data);
		}
	}	

	protected void selectZone(Zone zone, boolean arg1) {		
		zone.setSelected(arg1);
		
		if(arg1)
		{			
			Log.i("Zone_ListView","zone " + zone.getName() + " checked");
		}
		else
		{
			Log.i("Zone_ListView","zone " + zone.getName() + " unchecked");
		}
	}
}
